/*
    RExLib project, file rtcpsocket.h
    Copyright (C) 2012  Sarvaritdinov Ravil ra9oaj@gmail.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#ifndef RTCPSOCKET_H
#define RTCPSOCKET_H

#include "rabstractsocket.h"
#include <malloc.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <error.h>
#include <errno.h>

namespace rex
{

class RTcpSocket : public RAbstractSocket
{

public:
    RTcpSocket();
    RTcpSocket( const RTcpSocket &other);
    ~RTcpSocket();

    virtual uint16_t port() const;
    virtual const char* host ( AddressFormat format = RS_DOMAIN ) const;
    virtual int socketFd() const;
    virtual bool setSocketFd ( const int& sockfd );
    virtual bool connectToHost ( const char* host, const uint16_t& prt );
    virtual void closeSocket();
    virtual void setNonBlock ( bool flag = true );
    virtual bool setSocketLayer ( RAbstractSocket::AddressFamily _sl );
    virtual int status();
    virtual int error() const;
    virtual size_t write ( const char* buf, int len );
    virtual char* read ( int len = 0 );
    virtual size_t read ( char* buf, int len );
    virtual size_t bytesAvailable() const;
    virtual void clear();

protected:
  void setHostName( const char *host );
  void closeSockFd();

private:
    int _sfd;
    int _state;
    int _errno;
    char *_host;
    char *_num_addr;
    uint16_t _port;
    struct sockaddr *_addr;
};

}
#endif // RTCPSOCKET_H
